package cn.melon.mod.roll;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.common.ModelType;
import cn.melon.helper.BaseRequset;
import cn.melon.helper.IOnRequestListener;
import cn.melon.helper.LoggerHelper;
import cn.melon.helper.RequestHelper;
import cn.melon.mod.sc.StInfo;
import cn.melon.view.admin.roll.IUpdateRollTable;
import cn.melon.window.MainWindow;

public class RollRequest extends BaseRequset{

    private String TAG = LoggerHelper.makeLogTag(RollRequest.class);
    private Logger rollReqLogger = LoggerHelper.clientLogger();
    
    private IUpdateRollTable updateRollTable;
    
    MainWindow m;

    public RollRequest() {
        
    }
    
    public RollRequest(MainWindow m) {
        this.m = m;
    }
    
    public RollRequest(IUpdateRollTable updateRollTable) {
        this.updateRollTable = updateRollTable;
    }

    @Override
    public void makeRequset() {
        //getRollInfo();
        //delInfo("09014312");
        
        RollInfo info = new RollInfo("09014313", "无人", "213141744", 
                "男", "汉族", "团员", "未婚", "身份证栏项目", "出生日期栏项目", "籍贯栏项目",  
                "地址栏项目", "九龙湖校区", "计算机科学与工程学院", "计算机科学与技术", 
                "大三年级", "3班", "4年", "15189800598", "email", "签名", "2016-9-10");
        StInfo stInfo = new StInfo();
        info.setStudentNumber("09014315");
        info.setAddress("这是测试的地址");
        stInfo.setStudentNumber("09014311");
        stInfo.setClassTable("[\"6\"]");
        updateInfo(info, stInfo);
        
        
    //    updateInfo(info);
    //   getAllRollInfo();
    }   

    /**
     * 获取所有用户的学籍信息
     */
    public void getAllRollInfo() {
        
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "ALLROLL");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.ROLL).addRequest(jsonReq.toString())
                .addReceiver("allrollinfo", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        rollReqLogger.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {
                                
                                List<RollInfo> infoList = new ArrayList<>();
                                JSONArray jContent = jRet.getJSONArray("content");
                                for (int i = 0; i < jContent.length(); i++) {
                                    RollInfo info = RollInfo.toInfo(jContent.getJSONObject(i));
                                    infoList.add(info);
                                    rollReqLogger.debug(info.getAddress());
                                }
                                if (updateRollTable != null) {
                                    updateRollTable.updateTable(infoList);
                                }
                                
                            } else {
                                // 错误处理
                                rollReqLogger.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();

    }

    /**
     * 获取本人的学籍信息
     */
    public void getRollInfo() {
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "ASKINFO");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.ROLL).addRequest(jsonReq.toString())
                .addReceiver("rollinfo", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        rollReqLogger.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {
                                
                                // 工作暂时到此, 要求每个请求均可以做到返回对象
                                RollInfo rollInfo = RollInfo.toInfo(jRet.getJSONObject("content"));
                                rollReqLogger.debug(TAG + rollInfo.getCardNum());
                                if (m != null){
                                    m.setUserRollInfo(rollInfo);
                                    m.updateRollWindow();
                                }
                            } else {
                                // 错误处理
                                rollReqLogger.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
    }
    

    /**
     * 按照学号删除信息
     * @param studentNumber 学号信息
     */
    public void delInfo(String studentNumber) {
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "DELROLL");
            jsonReq.put("content", studentNumber);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.ROLL).addRequest(jsonReq.toString())
                .addReceiver("delRoll", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        rollReqLogger.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {

                            } else {
                                // 错误处理
                            	rollReqLogger.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
    }

    /**
     * 添加学籍信息
     * @param roll 学籍信息
     */
    public void addInfo(RollInfo roll) {
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "ADDROLL");
            jsonReq.put("Roll", roll.toJSON());
            
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.ROLL).addRequest(jsonReq.toString())
                .addReceiver("createRoll", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                    	rollReqLogger.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {

                            } else {
                                // 错误处理
                            	rollReqLogger.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
    }
    
    
    /**
     * 更新学籍信息与选课信息
     * @param info 学籍信息
     * @param stInfo 选课信息
     */
    public void updateInfo(RollInfo info, StInfo stInfo) {
        JSONObject jsonReq = new JSONObject();
        info.setCurClass("3");
        try {
            jsonReq.put("action", "UPDATEROLLST");
            jsonReq.put("content", info.toJSON());
            jsonReq.put("content_st", stInfo.toJSON());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.ROLL).addRequest(jsonReq.toString())
                .addReceiver("updateRollSt", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        rollReqLogger.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {
                                if (updateRollTable != null) {
                                    getAllRollInfo();       // 重新请求信息
                                }
                                
                                if (m != null) {
                                    System.out.println("更新学生信息");
                                    getRollInfo();          // 学生请求信息
                                }
                                JOptionPane.showMessageDialog(null, "更新信息成功");
                            } else {
                                // 错误处理
                                rollReqLogger.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
    };
    
    /**
     * @Title: updateInfo
     * @Description: 更新学籍信息
     * @param info void
     */
    public void updateInfo(RollInfo info) {
        JSONObject jsonReq = new JSONObject();
        info.setCurClass("3");
        try {
            jsonReq.put("action", "UPDATEROLL");
            jsonReq.put("content", info.toJSON());
            
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.ROLL).addRequest(jsonReq.toString())
                .addReceiver("updateRoll", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                    	rollReqLogger.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {
                                if (updateRollTable != null) {
                                    getAllRollInfo();       // 重新请求信息
                                }
                                
                                if (m != null) {
                                    System.out.println("更新学生信息");
                                    getRollInfo();          // 学生请求信息
                                }
                                JOptionPane.showMessageDialog(null, "更新信息成功");
                            } else {
                                // 错误处理
                            	rollReqLogger.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
    };
}
